package com.peng.leetcode.tree;

import java.util.ArrayList;
import java.util.List;

/**
 * LargestValues
 * 515. 在每个树行中找最大值
 * https://leetcode.cn/problems/find-largest-value-in-each-tree-row/
 * <p>
 * Created on 2022/12/5
 *
 * @author lupeng
 */
public class LargestValues1 {

    List<Integer> ans = new ArrayList<>();

    public List<Integer> largestValues(TreeNode root) {
        dfs(root, 0);
        return ans;
    }

    void dfs(TreeNode n, int height) {
        if (n == null) {
            return;
        }
        if (ans.size() == height) {
            ans.add(n.val);
        } else {
            ans.set(height, Math.max(ans.get(height), n.val));
        }
        dfs(n.left, height + 1);
        dfs(n.right, height + 1);
    }
}
